import requests
from urllib import request
import re
from io import BytesIO
import gzip
from bs4 import BeautifulSoup  
from distutils.filelist import findall
import json

#大陆：id 8 ,最大页数 114
#日韩：id 5 ,最大页数 56
#欧美：id 4 ,最大页数 47
#港台：id 13 ,最大页数 51
#团体组合：id 3 ,最大页数 10
#https://www.houyuantuan.com/mingxing/

id = '4'
maxPage = 47
type = '欧美'

headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',
        'Connection': 'keep-alive',
        'Referer': 'https://www.houyuantuan.com/mingxing/' + id + '/',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Host': 'www.houyuantuan.com',
        'Cookie': 'PHPSESSID=tnbr9ikmoc9nojik8q78tfoaj0; Hm_lvt_e54609bc582e9c0b9794754c0bd0cfe4=1579970646,1580021875; Hm_lpvt_e54609bc582e9c0b9794754c0bd0cfe4=1580023321',
        #cookie要重新获取
        'X-Requested-With': 'XMLHttpRequest'
    }

def add(name,avatar):
    r = requests.get('这里写你的添加请求，你也可以二开直接写入数据库' + name + '&avatar=' + avatar + '&type=' + type)
    print(str(r.content.decode('utf-8')))

r = request.Request('https://www.houyuantuan.com/mingxing/' + id + '/',headers=headers)
resp = request.urlopen(r)
contents = resp.read()
buff = BytesIO(contents)
f = gzip.GzipFile(fileobj=buff)
contents = f.read().decode('utf-8')
soup = BeautifulSoup(contents,"html.parser")
for tag in soup.find_all('a', class_='avatar'):
    name = tag.find('img').get('alt')       
    avatar = "https:" + tag.find('img').get('src')
    print( name + avatar )
    add(name,avatar)
    
for num in range(2,maxPage + 1):
    requests.packages.urllib3.disable_warnings()
    r = requests.get('https://www.houyuantuan.com/star/aindex/?id=' + id + '&page=' + str(num),headers=headers,verify=False)
    resp = str(r.content.decode('unicode-escape'))
    resp = json.loads(resp)
    for item in range(len(resp['data'])):
        name = resp['data'][item]['name']
        avatar = "https:" + resp['data'][item]['avatar']
        print( name + avatar )
        add(name,avatar)
        


    
